package model

import (
	"github.com/go-xorm/xorm"
	"global/structs"
	"github.com/shopspring/decimal"
)

type orderCoinMode struct{}

var OrderCoinModel = new(orderCoinMode)

// 插入数据
func (orderCoinMode) InsertItem(session *xorm.Session, orders *structs.OrderCoin) (int, error) {
	_, err := session.Insert(orders)
	if orders.Id > 0 {
		return orders.Id, nil
	}
	return 0, err
}

// 计算某个用户成交结果
func (orderCoinMode) CalcSumByTrust(trustType structs.TaskType, tid int) (decimal.Decimal, error) {

	order := new(structs.OrderCoin)
	if trustType == structs.TASK_BUY {
		num, err := MainDB.Where("buy_trust_id = ?", tid).Sum(order, "number")
		if err != nil {
			return decimal.Zero, err
		}
		return decimal.NewFromFloat(num), nil
	}

	num, err := MainDB.Where("sale_trust_id = ?", tid).Sum(order, "number")
	if err != nil {
		return decimal.Zero, err
	}

	return decimal.NewFromFloat(num), nil
}